﻿using System;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;

namespace Yglu.WebAdmin
{
	public partial class Terminals_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			CheckPermissions(AdminLevel.Company);

			if (!IsPostBack)
			{
				PanEdit.Visible = false;

				SetDefaults();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			if (PanGrid.Visible)
			{
				LoadData();
			}
		}

		private void SetDefaults()
		{
			SetDefaults(GridTerminals);

			TxtTerminalId.Enabled = false;
			
			if (SessionManager.LoggedUser.AdminLevel < AdminLevel.All)
			{
				TxtTerminalUniqueId.Enabled = false;
				CboStore.Enabled = false;
			}

			CboStore.DataSource = Settings.Stores;
			CboStore.DataBind();
		}

		private void LoadData()
		{
			if (SessionManager.LoggedStore.Id > 0)
			{
				GridTerminals.DataSource = new TerminalCollection(DataAccess.Terminal_Get(SessionManager.LoggedStore));
			}
			else
			{
				GridTerminals.DataSource = new TerminalCollection(DataAccess.Terminal_Get());
			}

			GridTerminals.DataBind();

			LblGridCount.Text = GetLocalResourceObject("LblGridCount.Text").ToString().Replace("#", GridTerminals.Rows.Count.ToString());

			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion

		#region Add and edit

		protected void ButAddNew_Click(Object sender, EventArgs e)
		{
			TxtTerminalId.Text = "0";
			TxtTerminalUniqueId.Text = Guid.NewGuid().ToString().ToUpper();
			ChkTerminalActive.Checked = true;

			LitEditLegend.Text = GetLocalResourceObject("LitAddTerminal.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		protected void GridTerminals_RowCommand(Object sender, GridViewCommandEventArgs e)
		{
			EditTerminal(Convert.ToInt32(e.CommandArgument));
		}

		private void EditTerminal(Int32 row)
		{
			Int32 id = Convert.ToInt32(GridTerminals.Rows[row].Cells[0].Text);
			Terminal t = new Terminal(id);

			TxtTerminalId.Text = t.Id.ToString();
			TxtTerminalUniqueId.Text = t.UniqueId;
			ChkTerminalActive.Checked = t.Active;

            LitEditLegend.Text = GetLocalResourceObject("LitEditTerminal.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		#endregion

		#region Save to database

		protected void ButSave_Click(Object sender, EventArgs e)
		{
			Page.Validate();

			if (!Page.IsValid)
			{
				return;
			}

			Terminal s = new Terminal(Convert.ToInt32(TxtTerminalId.Text));

			try
			{
				s.UniqueId = TxtTerminalUniqueId.Text.Trim();
				s.Active = ChkTerminalActive.Checked;

				try
				{
					DataAccess.Terminal_Upsert(s);

					TxtTerminalId.Text = s.Id.ToString();

					ClientScript.RegisterStartupScript(typeof(String), "TerminalSaved", "YgluTopAlert('" + Messages.ConfirmationDataSaved + "', true);", true);

					PanGrid.Visible = true;
				}
				catch (Exception ex)
				{
					Master.SetError(ex, Messages.ErrorCouldNotSaveData);
				}
			}
			catch (Exception ex)
			{
				Master.SetError(ex, Messages.ErrorInvalidForm);
			}
		}

		protected void ButCancel_Click(Object sender, EventArgs e)
		{
			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion
	}
}